package cn.edu.Day05.pm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;

public class BallPanel extends JPanel implements Runnable, MouseMotionListener {
    Ball ball;
    Random ran;
    Ban ban;
    boolean flag=true;
    public BallPanel()
    {
        ban=new Ban();
        ran=new Random();
        ball=new Ball();
        ball.setR(30);
        ball.setX(ran.nextInt(BallFrame.W-ball.getR()*2));
        ball.setY(ran.nextInt(BallFrame.H-ball.getR()*2));
        ball.setSpeed(ran.nextInt(3)+10);
        ball.setDirection(ran.nextInt(4));
        new Thread(this).start();
        this.addMouseMotionListener(this);
    }
    public void paint(Graphics g)
    {
        super.paint(g);
        this.setBackground(Color.BLACK);
        if(flag)
        {
            g.setColor(Color.pink);
            g.fillOval(ball.getX(),ball.getY(),ball.getR()*2,ball.getR()*2);
            g.setColor(Color.BLUE);
            g.fillRect(ban.getX(),ban.getY(),ban.getWidth(),ban.getHeight());
        }else{
            g.setFont(new Font("华文彩云",Font.BOLD,40));
            g.setColor(Color.RED);
            g.drawString("Game Over!!!",45,300);
        }
    }

    @Override
    public void run() {
        while (true)
        {

            switch ((ball.getDirection()))
            {
                case Ball.RIGHT_UP:
                    //右上
                    ball.setX(ball.getX()+ball.getSpeed());
                    ball.setY(ball.getY()-ball.getSpeed());
                    if(ball.getX()+ball.getR()*2>=BallFrame.W)
                    {
                        ball.setDirection(Ball.LEFT_UP);
                    }
                    if(ball.getY()<=0)
                    {
                        ball.setDirection(Ball.RIGHT_DWON);
                    }
                    break;
                    //右下
                case Ball.RIGHT_DWON:
                    ball.setX(ball.getX()+ball.getSpeed());
                    ball.setY(ball.getY()+ball.getSpeed());
                    if(ball.getX()+ball.getR()*2>=BallFrame.W)
                    {
                        ball.setDirection(Ball.LEFT_DWON);
                    }
                    if(ball.getY()+ball.getR()*2>=BallFrame.H)
                    {
                        ball.setDirection(Ball.RIGHT_UP);
                    }
                    break;
                    //左上
                case Ball.LEFT_UP:
                    ball.setX(ball.getX()-ball.getSpeed());
                    ball.setY(ball.getY()-ball.getSpeed());
                    if(ball.getY()<=0)
                    {
                        ball.setDirection(Ball.LEFT_DWON);
                    }
                    if(ball.getX()<=0)
                    {
                        ball.setDirection(Ball.RIGHT_UP);
                    }
                    break;
                    //左下
                case Ball.LEFT_DWON:
                    ball.setX(ball.getX()-ball.getSpeed());
                    ball.setY(ball.getY()+ball.getSpeed());
                    if(ball.getX()<=0)
                    {
                        ball.setDirection(Ball.LEFT_DWON);
                    }
                    if(ball.getY()+ball.getR()*2>=BallFrame.H)
                    {
                        ball.setDirection(Ball.LEFT_UP);
                    }
                    break;
            }
            repaint();
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        }
    }

    @Override
    public void mouseDragged(MouseEvent mouseEvent) {
//        System.out.println("mouseDrahed");
        ban.setX(mouseEvent.getX()-ban.getWidth()/2);
        int ballLeft=ball.getX();
        int ballRight=ball.getX()+ball.getR();
        int banLeft=ball.getX();
        int banRight=ball.getX()+ban.getWidth();
        if(ball.getY()+ball.getR()*2+10>=ban.getY()&&ball.getY()+ball.getR()*2-10<=ban.getY())
        {
            if(banRight>ballLeft&&banLeft<ballRight)
            {
                flag=true;
            }
            else {
                flag=false;
            }
        }


        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent mouseEvente) {
        System.out.println("mouseMoved");
    }
}
